﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;

namespace Eniux.Windows.ThreeDEngine.Base.CameraControllers
{
    /// <summary>
    /// 描述相机相关设置
    /// </summary>
    public class CameraSetting
    {
        #region 构造、析构

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="camera">摄像机</param>
        public CameraSetting(ProjectionCamera camera)
        {
            Position = camera.Position;
            LookDirection = camera.LookDirection;
            UpDirection = camera.UpDirection;
            NearPlaneDistance = camera.NearPlaneDistance;
            FarPlaneDistance = camera.FarPlaneDistance;
            var pcamera = camera as PerspectiveCamera;
            if (pcamera != null)
            {
                FieldOfView = pcamera.FieldOfView;
            }

            var ocamera = camera as OrthographicCamera;
            if (ocamera != null)
            {
                Width = ocamera.Width;
            }
        }

        #endregion

        #region 公共属性

        /// <summary>
        ///  获取或设置以世界坐标表示的摄像机位置
        /// </summary>
        public Point3D Position { get; set; }

        /// <summary>
        /// 获取或设置定义摄像机在世界坐标中的拍摄方向
        /// </summary>
        public Vector3D LookDirection { get; set; }

        /// <summary>
        ///  获取或设置定义摄像机向上方向
        /// </summary>
        public Vector3D UpDirection { get; set; }

        /// <summary>
        ///  获取或设置一个值，该值指定到摄像机近端剪裁平面的摄像机的距离
        /// </summary>
        public double NearPlaneDistance { get; set; }

        /// <summary>
        /// 获取或设置一个值，该值指定到摄像机远端剪裁平面的摄像机的距离
        /// </summary>
        public double FarPlaneDistance { get; set; }

        /// <summary>
        /// 获取或设置一个值，该值表示摄像机的水平视角
        /// </summary>
        public double FieldOfView { get; set; }

        /// <summary>
        /// 获取或设置摄像机取景框的宽度（单位为世界单位）
        /// </summary>
        public double Width { get; set; }

        #endregion

        #region 公共方法

        /// <summary>
        /// 更新摄像机设置
        /// </summary>
        /// <param name="camera">摄像机</param>
        public void UpdateCamera(ProjectionCamera camera)
        {
            camera.Position =Position;
            camera.LookDirection =LookDirection;
            camera.UpDirection =UpDirection;
            camera.NearPlaneDistance = NearPlaneDistance;
            camera.FarPlaneDistance = FarPlaneDistance;
            var pcamera = camera as PerspectiveCamera;
            if (pcamera != null)
            {
                pcamera.FieldOfView = this.FieldOfView;
            }

            var ocamera = camera as OrthographicCamera;
            if (ocamera != null)
            {
                ocamera.Width = this.Width;
            }
        }

        #endregion
    }
}
